package com.ruige.service.listenner;


import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * 监听机制监听当前服务的ip和port
 */
@Component
public class WebConfigListener implements ApplicationListener<WebServerInitializedEvent> {

    private int serverPort;

    public String getIp() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address.getHostAddress();
    }


    public String getPort() {
        return String.valueOf(this.serverPort);
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }
}
